 //
//  MYGMicrostationViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/8.
//

import UIKit
import QMUIKit
import BRPickerView

class MYGMicrostationViewController: MYGBaseController {
    var model = MYGFriendsModel()
    private var H:CGFloat = 900.0
    override func viewDidLoad() {
        super.viewDidLoad()

        congfigUI()
       
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        topView.setViewContent(model: model)
        getDoctorInfo()
    }
    
    func congfigUI(){
        title = "个人微站"
        view.backgroundColor = .white
        
        let rightItem = UIBarButtonItem(image: UIImage(named: "academicConference_share"), style: .plain, target: self, action: #selector(toShare))
        navigationItem.rightBarButtonItem = rightItem
        
        view.addSubview(contentScView)
        contentScView.addSubview(containerView)
        containerView.addSubview(topView)
        containerView.addSubview(toolView)
        containerView.addSubview(mineView)
        containerView.addSubview(dynamicView)
        contentScView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
       
        containerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(screenWidth)
            make.height.equalTo(H)
        }
        
        topView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(410)
        }
        
        toolView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(50)
            make.top.equalTo(topView.snp.bottom)
        }
        
        mineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(toolView.snp.bottom).offset(5)
            make.height.equalTo(1000)
            make.bottom.equalToSuperview()
        }
       
        dynamicView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(toolView.snp.bottom).offset(5)
            make.height.equalTo(800)
            make.bottom.equalToSuperview()
        }
        dynamicView.isHidden = true
        
        self.view.addSubview(sendDyBtn)
        sendDyBtn.snp.makeConstraints { make in
            make.width.height.equalTo(76)
            make.bottom.equalToSuperview().offset(-168)
            make.right.equalToSuperview()
        }
        
    }
 
    private lazy var contentScView:UIScrollView = {
        let vi = UIScrollView()
//        vi.showsVerticalScrollIndicator = false
        return vi
    }()
    
    private lazy var containerView:UIView = {
        let vi = UIView()
        vi.backgroundColor = .clear
        return vi
    }()
    
    private lazy var topView:MYGMincrostationTopView = {
        let view = MYGMincrostationTopView()
        view.selectIndex = { [self] index in
            toolView.changeButton(index)
        }
        return view
    }()
      
    private lazy var toolView:MYGMincrostationToolView = {
        let view = MYGMincrostationToolView()
        view.clickMenumBlock = { [self] value in
            mineView.isHidden = !value
            dynamicView.isHidden = value
            if(value) {
                mineView.reloadView(index: 0)
                contentScView.contentSize = CGSizeMake(screenWidth,H)
                containerView.snp.updateConstraints { make in
                    make.height.equalTo(H)
                }
                
            }else{
                dynamicView.relaodView(index: 0)
            }
        }
        view.clickMineButtonBlock = { [self]index in
            mineView.reloadView(index: index)
        }
        view.clickDyButtonBlock = { [self]index in
            dynamicView.relaodView(index: index)
        }
        return view
    }()
    
    private lazy var mineView:MYGAboutMineView = {
        let view = MYGAboutMineView()
        view.selectTypeBlock = { [self]value in
//            toolView.
            
        }
        return view
    }()

    private lazy var dynamicView:MYGMineDynamicView = {
        let view = MYGMineDynamicView()
        let doctor_id = model.id.isEmpty ? MYGUserManager.getUser()?.doctor.id : self.model.from_doctor_id
        view.doctor_id = "\(doctor_id ?? 0)"
        view.reloadSrcView = {[weak self] index in
            view.snp.updateConstraints { make in
                make.height.equalTo(index * 110 + 550)
            }
            self?.contentScView.contentSize = CGSizeMake(screenWidth,CGFloat(( index * 110) + 550))
        }
          
        return view
    }()
    private lazy var sendDyBtn: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "micr-add"), for: .normal)
        btn.layer.cornerRadius = 76/2
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(sendDyminBtn), for: .touchUpInside)
        return btn
    }()
   
    private lazy var showView:MYGMinAuthView = {
       let view = MYGMinAuthView()
        return view
    }()
}

extension MYGMicrostationViewController {
   
    @objc func toShare() {
        
        let textToShare:String = "医路通"
        let imageToShare:UIImage = UIImage(named: "AppIcon")!
    
        let doctor_id = model.id.isEmpty ? MYGUserManager.getUser()?.doctor.id : self.model.from_doctor_id

        let urlToShare:URL = URL(string: "\(shareUrl)/#/pages/shareH5/personalMicroWar?id=\(doctor_id ?? 0)")!

        let activityItems:Array = [textToShare, imageToShare, urlToShare] as [Any]
        let activityVC:UIActivityViewController = UIActivityViewController.init(activityItems: activityItems, applicationActivities: nil)
        activityVC.excludedActivityTypes = [UIActivity.ActivityType.postToFacebook,UIActivity.ActivityType.airDrop]

        navigationController?.present(activityVC, animated: true)
        
    }
    
    // 关于我
    func getDoctorInfo(){
        
        let doctor_id = model.id.isEmpty ? 0 : self.model.from_doctor.id
        MMYGNetwork<MYGMicrosrationApi,MYGAboutGDotorModel>.request(target: .aboutDoctor(type:0,doctor_id:"\(doctor_id)")) { [self] item in
            guard let item = item else  {
                return
            }
            
            if !item.introduction.isEmpty || !item.skilled.isEmpty{
                H = 1700
                containerView.snp.updateConstraints { make in
                    make.height.equalTo(H)
                }
            }
           
            mineView.model = item
            topView.amodel = item
          
        } failed: { _, error in
            
        }
    }
    
    @objc func sendDyminBtn() {
        
        BRStringPickerView.showPicker(withTitle: "", dataSourceArr: ["发表文字","发表动态"], select: 0) { [self] item in
            let vc = MYGFindSendController()
            if item?.value == "发表文字"{
                vc.isShowImageView = false
            }
            navigationController?.pushViewController(vc)
        }
    }
    
    @objc func showFasnView(){
        UIViewController.keyWindow?.addSubview(showView)
        showView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
    }
    
   
}
